<?php
namespace Home\Controller;

/**
 * 预定端房间相关接口
 * @modify trainyao 2016-05-04 创建
 */

class RoomController extends ParentController {

    /**
     * 获取客栈所有房型实时房态
     * @modify trainyao 2016-05-04    创建
     */
    public function roomTypes() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startDate' => $param['startDate'],
            'endDate' => $param['endDate'],
            'transCurrencyId' => $param['transCurrencyId'],
            'bookingLangSet' => LANG_SET,
        );

        // 是否在下单分割时间段之内
        $isSplitOrderTime = $this->isSplitOrderTime($param['hotel'], $param['startDate']);
        if ($isSplitOrderTime === true) {
            $RPCData['endDate'] = $param['endDate'] = date('Y-m-d', strtotime('+1 days', strtotime($param['endDate'])));
        }

        // 是否同一天
        if ($param['startDate'] == $param['endDate']) {
            returnJson(403, 700);
            return;
        }

        // 是否从钥匙包进入
        $qrclietBook = isset($_COOKIE['qrclient_ali_and_wechat']) ? true : false;
        $RPCData['qrclietBook'] = $qrclietBook;
        // 是否微信浏览器
        $isWechatBrowser = isWechatBrowser();
        $RPCData['isWechatBrowser'] = $isWechatBrowser;

        $result = $client->roomTypes($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取客栈所有房型实时房态
     * @modify trainyao 2016-05-04    创建
     */
    public function roomTypeDetail() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startDate' => $param['startDate'],
            'endDate' => $param['endDate'],
            'roomType' => $param['roomType'],
        );

        // 是否在下单分割时间段之内
        $isSplitOrderTime = $this->isSplitOrderTime($param['hotel'], $param['startDate']);
        if ($isSplitOrderTime === true) {
            $RPCData['endDate'] = $param['endDate'] = date('Y-m-d', strtotime('+1 days', strtotime($param['endDate'])));
        }

        // 是否同一天
        if ($param['startDate'] == $param['endDate']) {
            returnJson(403, 700);
            return;
        }

        $result = $client->roomTypeDetail($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取单个房间实时房态
     * @modify trainyao 2016-05-05    创建
     * @modify trainyao 2016-07-07    更名bookDetail,改为可不传房间ID,查看房型预定信息
     */
    public function bookDetail() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startDate' => $param['startDate'],
            'endDate' => $param['endDate'],
            'roomType' => $param['roomType'],
        );
        if (isset($param['room'])) {
            $RPCData['room'] = $param['room'];
        }
        // 是否在下单分割时间段之内
        $isSplitOrderTime = $this->isSplitOrderTime($param['hotel'], $param['startDate']);
        if ($isSplitOrderTime === true) {
            $RPCData['endDate'] = $param['endDate'] = date('Y-m-d', strtotime('+1 days', strtotime($param['endDate'])));
        }

        // 是否同一天
        if ($param['startDate'] == $param['endDate']) {
            returnJson(403, 700);
            return;
        }

        $result = $client->bookDetail($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            $result['data']['adminId'] = M('TbAdminHotel')->where(['role'=>1,'hotelentity_id'=>$param['hotel']])->getField('adminlogin_id');
            $result['data']['isSplitOrderTime'] = $isSplitOrderTime === true ? true : false;
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }


    /**
     * 获取客栈所有钟点房房型实时房态
     * @modify zhangzhen 2017-05-27    创建
     */
    public function hourRoomTypes() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startDate' => $param['startDate'],
            'endDate' => $param['endDate'],
            'hour' => $param['hour'],
            'transCurrencyId' => $param['transCurrencyId'],
            'bookingLangSet' => LANG_SET,
        );

        // 是否从钥匙包进入
        $qrclietBook = isset($_COOKIE['qrclient_ali_and_wechat']) ? true : false;
        $RPCData['qrclietBook'] = $qrclietBook;
        // 是否微信浏览器
        $isWechatBrowser = isWechatBrowser();
        $RPCData['isWechatBrowser'] = $isWechatBrowser;

        $result = $client->hourRoomTypes($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

     /**
     * 获取客栈所有房型实时房态
     * @modify zhangzhen 2017-06-01    创建
     */
    public function hourRoomTypeDetail() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startTime' => $param['startTime'],
            'hour' => $param['hour'],
            'roomType' => $param['roomType'],
        );
        $result = $client->hourRoomTypeDetail($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取单个房间实时房态
     * @modify zhangzhen 2017-06-01    创建
     */
    public function hourBookDetail() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startTime' => $param['startTime'],
            'hour' => $param['hour'],
            'roomType' => $param['roomType'],
        );
        if (isset($param['room'])) {
            $RPCData['room'] = $param['room'];
        }

        $result = $client->hourBookDetail($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            $result['data']['adminId'] = M('TbAdminHotel')->where(['role'=>1,'hotelentity_id'=>$param['hotel']])->getField('adminlogin_id');
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取客栈所有长租房实时房态
     * @modify zhangzhen 2017-07-28    创建
     */
    public function longRoomTypes() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startDate' => $param['startDate'],
            'month' => $param['month'],
            'transCurrencyId' => $param['transCurrencyId'],
            'bookingLangSet' => LANG_SET,
        );

        // 是否从钥匙包进入
        $qrclietBook = isset($_COOKIE['qrclient_ali_and_wechat']) ? true : false;
        $RPCData['qrclietBook'] = $qrclietBook;
        // 是否微信浏览器
        $isWechatBrowser = isWechatBrowser();
        $RPCData['isWechatBrowser'] = $isWechatBrowser;

        $result = $client->longRoomTypes($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取客栈所有房型实时房态
     * @modify zhangzhen 2017-06-01    创建
     */
    public function longRoomTypeDetail() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startDate' => $param['startDate'],
            'month' => $param['month'],
            'roomType' => $param['roomType'],
        );
        $result = $client->longRoomTypeDetail($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取房间实时房态
     * @modify zhangzhen 2017-06-01    创建
     */
    public function longBookDetail() {
        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('ROOM_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'hotel' => $param['hotel'],
            'startDate' => $param['startDate'],
            'month' => $param['month'],
            'roomType' => $param['roomType'],
        );
        if (isset($param['room'])) {
            $RPCData['room'] = $param['room'];
        }

        $result = $client->longBookDetail($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
            return;
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            $result['data']['adminId'] = M('TbAdminHotel')->where(['role'=>1,'hotelentity_id'=>$param['hotel']])->getField('adminlogin_id');
            returnJson(200, 200, $result['data']);
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }
}
